package org.jaaksi.customviewdemo;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

// 把产品给的内推cover zip包转为我们需要的格式
class PremoteUtil {
    public static void main(String[] args) {
        // dpi/语言/cover.jpg
        // 产品给的压缩包解压文件绝对路径
        String source = "/Users/yang/Desktop/bmi-内推更新-0813b";
        // PromoteServer cover所在绝对路径 xxx/PromoteServer/node/public/images/xxx/cover
        String dest = "/Users/yang/Projects/drojian/PromoteServer/node/public/images/bmi_calculator/cover";

        // cover/语言/dpi/cover.png
        extDownloadJson(source, dest);
    }

    private static void extDownloadJson(String sourceDir, String destDir) {
        File file = new File(sourceDir); // dpi/语言/cover.jpg
        for (String dpiDir : file.list()) { //dpi
            System.out.println("dpiDir = " + dpiDir);

            File file1 = new File(sourceDir, dpiDir);
            if (!file1.isDirectory()) continue;
            for (String langDir : file1.list()) { // 语言
//                if (langDir.equals(".DS_Store")) continue;
                if (!new File(file1, langDir).isDirectory()) continue;
                // 把dpi/语言/cover.jpg copy 到目标目录：语言/dpi/cover.png
                String source = sourceDir + "/" + dpiDir + "/" + langDir + "/cover.jpg";
                System.out.println("source = " + source);
                String dest = destDir + "/" + langDir + "/" + dpiDir + "/cover.png";
                File file2 = new File(dest);
                if (!file2.getParentFile().exists()){
                    file2.getParentFile().mkdirs();
                }


                System.out.println("dest = " + dest);
                copyNio(source, dest);

            }
        }
    }

    public static void copyNio(String source, String dest) {
        FileChannel input = null;
        FileChannel output = null;

        try {
            input = new FileInputStream(new File(source)).getChannel();
            output = new FileOutputStream(new File(dest)).getChannel();
            output.transferFrom(input, 0, input.size());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (input != null) {
                try {
                    input.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (output != null) {
                try {
                    output.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
